package classifier;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import model.BlogPost;
import model.Sentence;

/*
 * This class aims to serve as an adapter between the database and the local 
 */
public class DatabaseAdapter {

	private Connection conn = null;
	public static final String USRNAME = "root";
	public static final String PASSWD = "xsqj0123";

	public void initialize(String databaseURL) {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			// System.out.println("Attempting to connect to the database ... \n");
			conn = DriverManager.getConnection(databaseURL, USRNAME, PASSWD);
			// System.out.println("Database connection established.\n");
		} catch (InstantiationException e) {
			System.err.println(e.getMessage());
		} catch (IllegalAccessException e) {
			System.err.println(e.getMessage());
		} catch (ClassNotFoundException e) {
			System.err.println(e.getMessage());
		} catch (SQLException e) {
			System.err.println(e.getMessage());
		}
	}

	public boolean isConnected() {
		if (conn == null) {
			return false;
		} else {
			return true;
		}
	}

	public String getPostContent(String blogID) {
		String fullPost = "";
		Statement getSentences;
		try {
			getSentences = conn.createStatement();
			getSentences
					.executeQuery("select * from sentences where blog_id = '"
							+ blogID + "'");
			ResultSet returnedSentences = getSentences.getResultSet();
			while (returnedSentences.next()) {
				String content = returnedSentences.getString("content");
				fullPost = fullPost.concat(content);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fullPost;
	}

	public BlogPost getPost(String blogID, String annotator) {

		BlogPost post = null;
		Vector<Sentence> sentences = new Vector<Sentence>();

		Statement getPostStatement;
		try {
			getPostStatement = conn.createStatement();
			getPostStatement.executeQuery("SELECT * FROM post where id = '"
					+ blogID + "'");

			ResultSet returnedPosts = getPostStatement.getResultSet();
			returnedPosts.next();
			Statement getSentences = conn.createStatement();
			getSentences
					.executeQuery("select * from sentences where blog_id = '"
							+ blogID + "'");

			ResultSet returnedSentences = getSentences.getResultSet();
			while (returnedSentences.next()) {
				String content = returnedSentences.getString("content");
				Sentence sentence = new Sentence(content, Sentence.UNKNOWN,
						Sentence.UNKNOWN);
				int sequence = Integer.parseInt(returnedSentences
						.getString("sequence"));
				sentences.add(sequence, sentence);
			}

			Statement getSentenceLabels = conn.createStatement();
			getSentenceLabels
					.executeQuery("select * from labels where blog_id = '"
							+ blogID + "' and annotator = '" + annotator + "'");

			ResultSet returnedSentenceLabels = getSentenceLabels.getResultSet();
			if (returnedSentenceLabels.next()) {
				do {
					int topicality = Integer.parseInt(returnedSentenceLabels
							.getString("topicality"));
					int sentimentPolarity = Integer
							.parseInt(returnedSentenceLabels
									.getString("sentiment_polarity"));
					int sequence = Integer.parseInt(returnedSentenceLabels
							.getString("sequence"));
					sentences.get(sequence).setTopicality(topicality);
					sentences.get(sequence).setSentimentPolarity(
							sentimentPolarity);
				} while (returnedSentenceLabels.next());
			}
			getSentenceLabels.close();

			post = new BlogPost(blogID, sentences);
			int postTopicalityLabel = Integer.parseInt(returnedPosts
					.getString("topicality"));
			int postSentimentPolarityLabel = Integer.parseInt(returnedPosts
					.getString("sentiment_polarity"));
			post.setSentimentPolarityLabel(postSentimentPolarityLabel);
			post.setTopicality(postTopicalityLabel);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return post;
	}

	public Integer getSentimentPolarity(String blogID) {
		return null;
	}
}
